package com.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.entity.ShoppingCart;
import com.hotel.mapper.DishMapper;
import com.hotel.mapper.ShoppingCartMapper;
import com.hotel.service.ShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static java.lang.String.*;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author Lin
 * @since 2022-06-09
 */
@Service
public class ShoppingCartServiceImp extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Resource
    private ShoppingCartMapper mapper;


    @Override
    public IPage<ShoppingCart> getCartList(String nickName, Integer current, Integer size) {
        if (StringUtils.isNotEmpty(nickName)) {
            nickName=nickName.trim();
        }
        List<ShoppingCart> cartList = mapper.getCartList(nickName);
        List cartItems = new ArrayList<>();
        int curIdx = current > 1 ? (current - 1) * size : 0;
        for (int i = 0; i < size && curIdx + i < cartList.size(); i++) {
            cartItems.add(cartList.get(curIdx + i));
        }
        IPage page = new Page<>(current, size);
        page.setRecords(cartItems);
        page.setTotal(cartList.size());
        return page;
    }

    @Override
    public int updateCartNumber(Integer id,Integer number) {
        UpdateWrapper<ShoppingCart> cartUpdateWrapper = new UpdateWrapper<>();
        cartUpdateWrapper.eq("id",id);
        cartUpdateWrapper.set("number",number);
        int update = this.baseMapper.update(null, cartUpdateWrapper);
        return update;
    }


    @Override
    public int deleteCart(String id) {
        return this.baseMapper.deleteById(id);
    }

    @Override
    public int clearCart(String nickName) {
        return mapper.clearCart(nickName);
    }
}
